package tech.wenxu.consumerhystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import tech.wenxu.consumerhystrix.fegin.HelloFeginService;

/**
 * @author zhaowenxu
 * @date 2020/4/12 0:06
 */
@RestController
public class HelloController {

    @Autowired
    private HelloFeginService helloFeginService;

    @GetMapping("/hello/{name}")
    @HystrixCommand(fallbackMethod = "fallbackHello")
    public String hello(@PathVariable String name) {
        return helloFeginService.hello(name);
    }

    /**
     * 断路器方法，返回值和方法一致，参数也一致
     * 当发生异常时，直接返回默认值，这种形式称为：降级
     * 注意一个bug，断路器刚启动时，第一次访问就到断路器，第一次进入断路器不算数。
     */
    public String fallbackHello(String name) {
        return "tony";
    }
}
